﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Data;

namespace NET.Library.Config
{
    /// <summary>
    /// Helper class for working with an application's configuration's app settings.
    /// </summary>
    public static class ConfigManager
    {
        /// <summary>
        /// 
        /// </summary>
        public static NameValueCollection AppSettings = ConfigurationManager.AppSettings;

        /// <summary>
        /// Adds the ConfigurationManager.AppSettings NameValueCollection to a DataTable.
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAppSettingsAsDataTable()
        {
            NameValueCollection appSettings = AppSettings;
            string[] keys = appSettings.AllKeys;
            
            DataTable dt = new DataTable();
            dt.Columns.Add("key");
            dt.Columns.Add("value");

            for (int i = 0; i < appSettings.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["key"] = keys[i];
                dr["value"] = appSettings[i];

                dt.Rows.Add(dr);
            }

            return dt;
        }

        /// <summary>
        /// Adds the ConfigurationManager.AppSettings NameValueCollection to a DataSet.
        /// </summary>
        /// <returns></returns>
        public static DataSet GetAppSettingsAsDataSet()
        {
            DataSet ds = new DataSet();
            ds.Tables.Add(GetAppSettingsAsDataTable());
            return ds;
        }

        /// <summary>
        /// Adds the ConfigurationManager.AppSettings NameValueCollection to a DataTable.
        /// </summary>
        /// <param name="keyColumnName">Specify your own column name for the key.</param>
        /// <param name="valueColumnName">Specify your own column name for the value.</param>
        /// <returns></returns>
        public static DataTable GetAppSettingsAsDataTable(string keyColumnName, string valueColumnName)
        {
            NameValueCollection appSettings = AppSettings;
            string[] keys = appSettings.AllKeys;

            DataTable dt = new DataTable();
            dt.Columns.Add(keyColumnName);
            dt.Columns.Add("value");

            for (int i = 0; i < appSettings.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr[keyColumnName] = keys[i];
                dr[valueColumnName] = appSettings[i];

                dt.Rows.Add(dr);
            }

            return dt;
        }

        /// <summary>
        /// Adds the ConfigurationManager.AppSettings NameValueCollection to a DataSet.
        /// </summary>
        /// <param name="keyColumnName">Specify your own column name for the key.</param>
        /// <param name="valueColumnName">Specify your own column name for the value.</param>
        /// <returns></returns>
        public static DataSet GetAppSettingsAsDataSet(string keyColumnName, string valueColumnName)
        {
            DataSet ds = new DataSet();
            ds.Tables.Add(GetAppSettingsAsDataTable(keyColumnName, valueColumnName));
            return ds;
        }
    }
}
